package org.lucky.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import org.lucky_common.domain.ChartLine;
import org.lucky_common.domain.InviteParam;
import org.lucky_common.domain.LuckyShare;
import org.lucky_common.domain.LuckyShareCount;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LuckyMapper extends BaseMapper<LuckyShare> {
    @Select("SELECT ls.*, ti.* FROM `lucky_share` ls JOIN t_invite ti ON ls.inviteCode = ti.invitedCode WHERE ls.inviteCode = #{inviteCode}")
    @Results(id = "LuckyShareMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "inviteCode", column = "inviteCode"),
            @Result(property = "inviteCode2", column = "inviteCode2"),
            @Result(property = "count", column = "count"),
            @Result(property = "verifyCount", column = "verifyCount"),
            @Result(property = "userId", column = "userId"),
            @Result(property = "parentId", column = "parentId"),
            @Result(property = "submitParam", column = "submitParam"),
            @Result(property = "inviteParam", column = "invitedCode", javaType = InviteParam.class,
                    one = @One(select = "selectInviteParam", fetchType = FetchType.EAGER ))
    })
    LuckyShare selectOneWithInvite(String inviteCode);

    @Select("SELECT * FROM t_invite WHERE invitedCode = #{invitedCode}")
    @Results(value = {
            @Result(property = "id", column = "id"),
            @Result(property = "count2", column = "count2"),
            @Result(property = "effectiveCount", column = "effectiveCount"),
            @Result(property = "invitedCode", column = "invitedCode"),
            @Result(property = "parentId", column = "parentId")
    })
    InviteParam selectInviteParam(String invitedCode);

    @Select("SELECT COUNT(r.verifyCount) rid,r.`inviteCode` FROM lucky_share r WHERE r.verifyCount >= 3  GROUP BY r.id")
    @Results(value = {
            @Result(property = "value", column = "rid"),
            @Result(property = "inviteCode", column = "inviteCode"),
    })
    List<LuckyShareCount> getLuckyShareCount();

    @Select("SELECT COUNT(r.verifyCount) rid,r.`inviteCode` FROM lucky_share r WHERE r.verifyCount IS NULL or r.verifyCount BETWEEN 0 AND 2 GROUP BY r.id")
    @Results(value = {
            @Result(property = "value", column = "rid"),
            @Result(property = "inviteCode", column = "inviteCode"),
    })
    List<LuckyShareCount> getLuckyShareCount2();


    @Select("SELECT CASE DAYOFWEEK(STR_TO_DATE(data, '%Y-%m-%d')) WHEN 1 THEN 'Sunday' WHEN 2 THEN 'Monday' WHEN 3 THEN 'Tuesday' WHEN 4 THEN 'Wednesday' WHEN 5 THEN 'Thursday' WHEN 6 THEN 'Friday' WHEN 7 THEN 'Saturday' END AS day_of_week,DATE_FORMAT(STR_TO_DATE(data, '%Y-%m-%d'), '%Y-%m-%d') AS date,COUNT(*) AS count FROM lucky_share GROUP BY DATE_FORMAT(STR_TO_DATE(data, '%Y-%m-%d'), '%Y-%m-%d') ORDER BY FIELD(DAYOFWEEK(STR_TO_DATE(data, '%Y-%m-%d')), 2, 3, 4, 5, 6, 7, 1);")
    @Results(value = {
            @Result(property = "day_of_week", column = "day_of_week"),
            @Result(property = "date", column = "date"),
            @Result(property = "count", column = "count"),
    })
    List<ChartLine> getChartLine();
}
